package struts.action;

import java.io.IOException;
import java.net.URL;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import businss.PanduanAction;
import service.vo.MessageVO;
import struts.form.ActionForm;
import struts.form.FullForm;
import struts.util.XmlBean;

/**
 * Servlet implementation class ActionServlet
 */
public class ActionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ActionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String path = getPath(request.getRequestURI());
		Map map = (Map) this.getServletContext().getAttribute("struts");
		Map<String, Class> actionMap = (Map<String, Class>) map.get("action");
		Map<String, Class> beanMap = (Map<String, Class>) map.get("bean");
		Map<String, Map<String,String>> forwardMap = (Map<String, Map<String, String>>) map.get("forward");
		
		ActionForm form = FullForm.full(beanMap.get(path), request);
		
		Class clazz = actionMap.get(path);
		Action action = null;
		String url = "";
		try {
			action = (Action) clazz.newInstance();
			Map<String, String> forward = forwardMap.get(path);
			url = action.execute(request, form, forward);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		RequestDispatcher dis = request.getRequestDispatcher(url);
		dis.forward(request, response);
		
//		String path = getPath(request.getRequestURI());
//		Map<String, XmlBean> map = (Map<String, XmlBean>) this.getServletContext().getAttribute("struts");
//		XmlBean xmlBean = map.get(path);
//		String formClass = xmlBean.getFormClass();
//		ActionForm form = FullForm.full(formClass, request);
//		
//		String actionClass = xmlBean.getActionClass();
//		Action action = null;
//		String url = "";
//		try {
//			Class clazz = Class.forName(actionClass);
//			action = (Action) clazz.newInstance();
//			url = action.execute(request,form,xmlBean.getActionForward());
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		RequestDispatcher dis = request.getRequestDispatcher(url);
//		dis.forward(request, response);
		
//		Action action = new PanduanAction();
//		String url = action.execute(request);
//		//获得转发器对象
//		RequestDispatcher dis = request.getRequestDispatcher(url);
//		dis.forward(request, response);
//		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	private String getPath(String path){
		return path.split("\\.")[0];
	}

}
